package com.vay.de.dang.online.web;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vay.de.dang.online.entiy.AddressEntity;
import com.vay.de.dang.online.entiy.MenuItemEntity;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.util.VnContext;

public class TableApi extends BaseApi {

    //////////////// 个人信息 /////////////////
    // 居住时长
    public static final String LIVE_YEARS = "LIVE_YEARS";
    // 居住类型
    public static final String LIVE_TYPE = "RESIDENTIAL_TYPE";
    // 性别
    public static final String GENDER = "GENDER";
    // 最高学历
    public static final String HIGHEST_EDUCATION = "HIGHEST_EDUCATION";
    // 婚姻状态
    public static final String MARITAL_STATUS = "MARITAL_STATUS";
    // 子女数量
    public static final String NUMBER_OF_CHILDREN = "NUMBER_OF_CHILDREN";
    // 联系人关系
    public static final String EMERGENCY_CONTACT_RELATIONSHIP = "EMERGENCY_CONTACT_RELATIONSHIP";

    //////////////// 公司信息 //////////////////
    // 职业
    public static final String OCCUPATION = "OCCUPATION";
    // 月收入
    public static final String MONTHLY_INCOME = "MONTHLY_INCOME";
    // 工作时长
    public static final String WORKING_YEARS = "WORKING_YEARS";
    // 发薪日
    public static final String PAY_DAY = "PAY_DAY";

    //////////////// 收款信息 //////////////////
    // 取款方式
    public static final String WITHDRAWAL_TYPE = "WITHDRAWAL_TYPE";
    // 取款方式: 银行
    public static final String WITHDRAWAL_METHOD = "WITHDRAWAL_METHOD";
    // 取款方式: 电子银行
    public static final String ELECTRONIC_WALLET = "ELECTRONIC_WALLET";
    // 门店名称
    public static final String STORE_NAME = "STORE_NAME";

    // 证件类型
    public static final String ID_TYPE = "ID_TYPE";

    private static List<AddressEntity> ProvinceList = new ArrayList<>();
    private static Map<String, List<AddressEntity>> RegionList = new HashMap<>();

    // 缓存
    private static Map<String, List<MenuItemEntity>> CodeTableCache = new HashMap<>();

    public static List<MenuItemEntity> getCodeTable(String codeType) {
        return CodeTableCache.get(codeType);
    }

    // 码表
    public static void codeTable(String codeType, ReqCallback callback) {
        List<MenuItemEntity> cache = CodeTableCache.get(codeType);
        if (!VnUtil.isEmpty(cache)) {
            callback.callback(ReqResult.ok(cache));
        } else {
            execTask(new WorkerBase(callback) {
                @Override
                public ReqResult doTask() {
                    try {
                        String language = VnContext.instance().getLanguage();
                        //String url = wrapperUrl("wz/syscode/querySelectOptionInfo/" + codeType + "/" + language);
                        String url = wrapperUrl("FGpO/PrTmmjg/YUF/" + codeType + "/" + language);
                        String json = doGet(url);
                        if (JsonUtil.isOk(json)) {
                            List<MenuItemEntity> list = JsonUtil.toOptionItems(json);
                            CodeTableCache.put(codeType, list);
                            return ReqResult.ok(list);
                        } else {
                            return ReqResult.error(JsonUtil.readMsg(json));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
        }
    }

    // 码表
    public static void provinceTable(ReqCallback callback) {
        if (!ProvinceList.isEmpty()) {
            callback.callback(ReqResult.ok(ProvinceList));
        } else {
            execTask(new WorkerBase(callback) {
                @Override
                public ReqResult doTask() {
                    try {
                        Map<String, Object> param = new HashMap<>();
                        param.put("addressCode", "");
                        param.put("addressLevel", 1);
                        param.put("addressName", "");
                        param.put("parentCode", "");
                        String url = wrapperUrl("KmvBBnB/FzOiqqLC/yyq");
                        //String url = wrapperUrl("loanSupermarket/incomingParts/commonAddress");
                        String str = JsonUtil.objectToJson(param);
                        String json = doPostByJson(url, str);
                        if (JsonUtil.isOk(json)) {
                            List<AddressEntity> list = JsonUtil.toCity(json);
                            if (list != null) {
                                ProvinceList.clear();
                                ProvinceList.addAll(list);
                                return ReqResult.ok(list);
                            }
                        } else {
                            return ReqResult.error(JsonUtil.readMsg(json));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
        }
    }

    public static void regionTable(int level, String parentCode, ReqCallback callback) {
        final String key = level + "_" + parentCode;
        List<AddressEntity> list = RegionList.get(key);
        if (list != null && list.size() > 0) {
            callback.callback(ReqResult.ok(list));
        } else {
            execTask(new WorkerBase(callback) {
                @Override
                public ReqResult doTask() {
                    try {
                        Map<String, Object> param = new HashMap<>();
                        param.put("addressCode", "");
                        param.put("addressLevel", level);
                        param.put("addressName", "");
                        param.put("parentCode", parentCode);
                        //String url = wrapperUrl("loanSupermarket/incomingParts/commonAddress");
                        String url = wrapperUrl("KmvBBnB/FzOiqqLC/yyq");
                        String str = JsonUtil.objectToJson(param);
                        String json = doPostByJson(url, str);
                        if (JsonUtil.isOk(json)) {
                            List<AddressEntity> list = JsonUtil.toCity(json);
                            RegionList.put(key, list);
                            return ReqResult.ok(list);
                        } else {
                            return ReqResult.error(JsonUtil.readMsg(json));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
        }
    }
}
